/*
       参数解释：
       title	标题
       url		请求的url
       id		需要操作的数据id
       w		弹出层宽度（缺省调默认值）
       h		弹出层高度（缺省调默认值）
   */
$(function () {
    var $table = $("#table");
    var _table = $table.dataTable($.extend(true, {}, CONSTANT.DATA_TABLES.DEFAULT_OPTION, {
        ajax: function (data, callback, settings) {
            //封装请求参数
            var param = userManage.getQueryCondition(data);
            $.ajax({
                type: "POST",
                url: "/errorInfo/selectErrorInfoByPage",
                cache: false,  //禁用缓存
                data: param,    //传入已封装的参数
                dataType: "json",
                success: function (result) {
                    //异常判断与处理
                    if (result.errorCode) {
                        alert("查询失败");
                        return;
                    }
                    //封装返回数据
                    var returnData = {};
                    returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                    returnData.recordsTotal = result.total;//总记录数
                    returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
                    returnData.data = result.pageData;
                    //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
                    //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕
                    callback(returnData);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("查询失败");
                }
            });
        },
        //绑定数据
        columns: [
            {
                data: "id",//终端编号
                orderable: true,
            },
            {
                data: "sno",//终端编号
                orderable: true,
            },
            {
                data: "terminalType",//终端标识码
                orderable: true,
            },
            {
                data: "imei",//终端标识码
                orderable: true,
            },
            {
                data: "appVersion",//pos号
                orderable: true,
            },
            {
                data: "apiVersion",//pos号
                orderable: true,
            },
            {
                data: "clientType",//终端编号
                orderable: true,
            },
            {
                data: "clientMark",//终端标识码
                orderable: true,
            },
            {
                data: "errorInfo",//
                orderable: true,
                render: function (data, type, row, meta) {
                    var info = row.errorInfo;
                    if (info.length > 100) {
                        info = info.substr(0, 100);
                    }
                    return (
                        "<a onclick=\"layer_show('错误详情：（ID：" + row.id + "）','/errorInfo/detail?id=" + row.id + "','800','630')\">" + info + "</a>\n"

                    );
                }
            },
            {
                data: "userAgent",//pos号
                orderable: true,
            },
            {
                data: "clientIp",//pos号
                orderable: true,
            },
            {
                data: "errorTime",//pos号
                orderable: true,
            },
            {
                data: "createTime",//pos号
                orderable: true,
            }

        ]
    })).api();//此处需调用api()方法,否则返回的是JQuery对象而不是DataTables的API对象

    //查询
    $("#btn_select").click(function () {
        _table.draw();
    });
});

/*商户-增加*/
function errorDetail(id) {
    layer_show("错误详情", '/errorInfo/detail?id=' + id, 800, 600);
}

var userManage = {
    getQueryCondition: function (data) {
        var param = {};
        //组装排序参数
        if (data.order && data.order.length && data.order[0]) {
            switch (data.order[0].column) {
                case 0:
                    param.orderColumn = "id";//
                    break;
                case 1:
                    param.orderColumn = "sno";//
                    break;
                case 2:
                    param.orderColumn = "terminal_type";//
                    break;
                case 3:
                    param.orderColumn = "imei";//
                    break;
                case 4:
                    param.orderColumn = "app_version";//
                    break;
                case 5:
                    param.orderColumn = "api_version";//
                    break;
                case 6:
                    param.orderColumn = "client_type";//
                    break;
                case 7:
                    param.orderColumn = "client_mark";//
                    break;
                case 8:
                    param.orderColumn = "error_info";//
                    break;
                case 9:
                    param.orderColumn = "user_agent";//
                    break;
                case 10:
                    param.orderColumn = "client_ip";//
                    break;
                case 11:
                    param.orderColumn = "error_time";//
                    break;
                case 12:
                    param.orderColumn = "create_time";//
                    break;
                default:
                    param.orderColumn = "id";//
                    break;
            }
            //排序方式asc或者desc
            param.orderDir = data.order[0].dir;
        }
        param.groupName = $("#groupName").val();//查询条件
        //组装分页参数
        param.startIndex = data.start;
        param.pageSize = data.length;
        param.draw = data.draw;
        param.sno = $("#txtSno").val();
        param.imei = $("#txtImei").val();
        param.errorInfo = $("#txtErrorInfo").val();
        param.createTime = $("#startTime").val();
        param.clientIp = $("#endTime").val();
        return param;
    }
};

/*商户-增加*/
function terminal_add(title, url, w, h) {
    layer_show(title, url, w, h);
}

/*管理员-编辑*/
function terminal_edit(title, url, id, w, h) {
    layer_show(title, url, w, h);
}

$("#btnExportLog").click(function () {
    layer.msg("正在导出，请稍等", {icon: 0, time: 5000});
    var sno = $("#txtSno").val();
    var imei = $("#txtImei").val();
    var errorInfo = $("#txtErrorInfo").val();
    var createTime = $("#startTime").val();
    var clientIp = $("#endTime").val();
    var url = "/errorInfo/exportLog?sno=" + sno + "&imei=" + imei + "&errorInfo=" + errorInfo + "&createTime=" + createTime + "&clientIp=" + clientIp;
    window.location.href = url;
})